<?php
namespace admin\helper;

class country  extends common {
      public $table ='zone';

      public function lists()
      {
           $data=  $this->db->pageSelect($this->table,'*',['parent_id'=>0,'ORDER'=>['id_zone'=>"DESC"]],20);
           if($data['goodsList'])
           {
               $county_id = array_unique(array_column($data['goodsList'],'id_country'));
               //获取国家
               $country = $this->getCountry($county_id);

               //获取货币
               $currency_id =  array_column($data['goodsList'],'currency_id');
               $currency = $this->getCurrency($currency_id);

               //获取子区域
               $zone = $this->getSon(array_column($data['goodsList'],'id_zone'));

               foreach ($data['goodsList'] as $row)
               {
                  $row['country'] = $country[$row['id_country']]['title'];
                  $row['currency'] = $currency[$row['currency_id']]['currency_code'];
                  $row['son'] =   $zone[$row['id_zone']] ;
                  $info[] = $row;
               }
               $data['goodsList'] = $info;
           }
          return $data;
      }

      public function getSon($id_zone =[])
      {
          $map =['parent_id'=>$id_zone];
          $ret = $this->db->select('zone',['title','parent_id','id_zone','lang'],$map);
          if($ret)
          {
              foreach ($ret as $value)
              {
                  $parent_id = $value['parent_id'] ;
                  $data[$parent_id][] = $value;
              }
             
          }
          return $data;
      }

      public function getCountry($id_country)
      {
          $map =['id_country'=>$id_country];
          $ret = $this->db->select('country',['title','id_country'],$map);
          if($ret)
          {
              $ret = array_column($ret,NULL,'id_country') ;
          }
      
          return $ret;
      }
      public function getCurrency($id_currency)
      {
          $map =['currency_id'=>$id_currency];
          $ret = $this->db->select('currency',['currency_code','currency_id'],$map);
          if($ret)
          {
              $ret = array_column($ret,NULL,'currency_id') ;
          }

          return $ret;
      }

      public function getOne($id)
      {
          return $this->db->get($this->table,'*',['id_zone'=>$id]);
      }

      public function getAllCountry()
      {
          $ret = $this->db->select('country',['title','id_country']);
          return $ret;
      }

      public function getAllCurrency(){
          $ret = $this->db->select('currency',['currency_code','currency_id','currency_title']);
          return $ret;
      }

      public function getAllZone($filter=""){
          $map  = ['parent_id'=>0];
          if($filter)
          {
              $map = $filter;
          }
          $ret = $this->db->select('zone',['id_zone','title','id_country'],$map);
          return $ret;
      }

      public function update($data){
          //更新
          if($data['id'])
          {
              $map['id_zone']  = $data['id'];
              unset($data['id']);
              $ret = $this->db->update($this->table,$data,$map);
          }
          else{
              unset($data['id']);
              $ret = $this->db->insert($this->table,$data);
          }
          
          return ['ret'=>1,'msg'=>"OK"];
      }

      public function delete($id){
          //检查产品是否存在删除地区
          $ret = $this->db->select('product','*',['id_zone'=>$id,'is_del'=>0]);
          if($ret)
          {
              return ['ret'=>0,'msg'=>'禁止删除，存在含有正在删除地区的产品'];
          }
          //检查是否有子区域
          $ret =  $this->db->select('zone','*',['parent_id'=>$id]);
          if($ret)
          {
              return ['ret'=>0,'msg'=>'禁止删除，存在含有正在删除地区的下级区域'];
          }

          //删除
          $this->db->delate('zone',['id_zone'=>$id]);
          return ['ret'=>1,'msg'=>'删除成功'];
      }

}